package com.ganzalang.gmall.interfacespecification.second.comtroller;

import com.ganzalang.gmall.interfacespecification.second.domain.User;
import com.ganzalang.gmall.interfacespecification.second.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;

/**
 * @description:
 * @author: Ganzalang
 * @Date 2021/3/30 20:29
 */
@Data
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/addUser")
    public String addUser(@RequestBody @Valid User user, BindingResult bindingResult) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            return error.getDefaultMessage();
        }

        return userService.addUser(user);
    }

}
